package sync;


/**
 * @author Maxim K
 * This class helps to enforce safety in concurrent environment.  
 * 
 */
public class ReadWriteGuard {
	
	
	protected int activeReaders_;
	protected int activeWriters_;
	protected int waitingWriters_;
	protected int waitingReaders_;
	
	
	/**
	 * class Constructor
	 */
	public ReadWriteGuard(){
		this.activeReaders_=0;
		this.activeWriters_=0;
		this.waitingReaders_=0;
		this.waitingWriters_=0;
	}

	
	/**
	 * 
	 * @return
	 * 			true if the current thread can read from the object.
	 */
	protected boolean allowReader(){
		return this.waitingWriters_==0 & this.activeWriters_==0;
	}
	
	/**
	 * 
	 * @return	
	 * 			true if the current thread can write to the object.
	 */
	protected boolean allowWriters(){
		return this.activeReaders_==0 & this.activeWriters_==0;
	}
	
}
